/* $Id: SingleClientCompanyDomainObjectManagerImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.domain.manager.provider;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ae.pcfc.etks.online.common.ConstantsCommon;
import ae.pcfc.etks.online.common.dao.SingleClientCompanyDAO;
import ae.pcfc.etks.online.common.dao.SingleClientCompanyRoleDAO;
import ae.pcfc.etks.online.common.domain.ClientCompanyReq;
import ae.pcfc.etks.online.common.domain.ClientCompanyRoleReq;
import ae.pcfc.etks.online.common.domain.TTscClientCategory;
import ae.pcfc.etks.online.common.domain.TTscClientCompany;
import ae.pcfc.etks.online.common.domain.TTscLicType;
import ae.pcfc.etks.online.common.domain.manager.SingleClientCompanyDomainObjectManager;
import ae.pcfc.etks.online.common.exception.LookupException;
import ae.pcfc.etks.online.common.exception.PersistenceException;
import ae.pcfc.etks.online.common.vo.ActivityVO;
import ae.pcfc.etks.online.common.vo.ClientNameVO;
import ae.pcfc.etks.online.common.vo.LicenseVO;
import ae.pcfc.etks.online.common.vo.NonClientCompanyVO;
import ae.pcfc.etks.online.common.vo.SCClientCompanyRoleVO;
import ae.pcfc.etks.online.common.vo.SearchCriteriaActivityVO;
import ae.pcfc.etks.online.common.vo.SearchCriteriaCompanyVO;
import ae.pcfc.etks.online.common.vo.TrakheesIdAndPINVO;

public class SingleClientCompanyDomainObjectManagerImpl implements SingleClientCompanyDomainObjectManager {

	private SingleClientCompanyDAO singleClientCompanyDAO;
	private SingleClientCompanyRoleDAO singleClientCompanyRoleDAO;

	/**
	 * setSingleClientCompanyRoleDAO
	 * 
	 * @param singleClientCompanyRoleDAO
	 * void
	 */
	public void setSingleClientCompanyRoleDAO(SingleClientCompanyRoleDAO singleClientCompanyRoleDAO) {
		this.singleClientCompanyRoleDAO = singleClientCompanyRoleDAO;
	}

	/**
	 * setSingleClientCompanyDAO
	 * 
	 * @param singleClientCompanyDAO
	 * void
	 */
	public void setSingleClientCompanyDAO(SingleClientCompanyDAO singleClientCompanyDAO) {
		this.singleClientCompanyDAO = singleClientCompanyDAO;
	}

	/**
	 * add
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.BaseDomainObjectManager#add(java.lang.Object)
	 * 
	 */
	public <T> void add(T obj) throws PersistenceException {
		// TODO Auto-generated method stub

	}

	/**
	 * update
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.BaseDomainObjectManager#update(java.lang.Object)
	 * 
	 */
	public <T> void update(T obj) throws PersistenceException {
		// TODO Auto-generated method stub

	}

	/**
	 * delete
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.BaseDomainObjectManager#delete(java.lang.Object)
	 * 
	 */
	public <T> void delete(T obj) throws PersistenceException {
		// TODO Auto-generated method stub

	}

	/**
	 * findByPrimaryKey
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.BaseDomainObjectManager#findByPrimaryKey(java.lang.String)
	 * 
	 */
	public <T> T findByPrimaryKey(String id) throws LookupException {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * findAll
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.BaseDomainObjectManager#findAll()
	 * 
	 */
	public <T> Collection<T> findAll() throws LookupException {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * add
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientCompanyDomainObjectManager#add(ae.pcfc.etks.online.common.domain.ClientCompanyReq)
	 * 
	 */
	public Map<String, String> add(ClientCompanyReq companyReq) throws PersistenceException {

		String trakheesId = singleClientCompanyDAO.add(companyReq);

		// Inserting the Roles in the TSC
		//for (ClientCompanyRoleReq companyRole : companyReq.getClientCompanyRoleReqs()) {
		//	this.singleClientCompanyRoleDAO.add(new Long(companyRole.getCsrReqId()), trakheesId, companyReq.getCpCreatedUser());
		//}

		String pinCode = this.singleClientCompanyDAO.getPinCodeByTrakheesId(trakheesId);

		Map<String, String> values = new HashMap<String, String>();
		values.put(ConstantsCommon.TRAKHEES_ID, trakheesId);
		values.put(ConstantsCommon.PIN_CODE, pinCode);

		return values;

	}

	/**
	 * findSCCompanyByTrakheesId
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientCompanyDomainObjectManager#findSCCompanyByTrakheesId(java.lang.String)
	 * 
	 */
	public TTscClientCompany findSCCompanyByTrakheesId(String trakheesId) {
		return this.singleClientCompanyDAO.findSCCompanyByTrakheesId(trakheesId);
	}

	/**
	 * findSCCompanyRoleByTrakheesId
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientCompanyDomainObjectManager#findSCCompanyRoleByTrakheesId(java.lang.String)
	 * 
	 */
	public List<SCClientCompanyRoleVO> findSCCompanyRoleByTrakheesId(String trakheesId) {
		return this.singleClientCompanyRoleDAO.findSCCompanyRoleByTrakheesId(trakheesId);
	}

	/**
	 * addSCCompanyRole
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientCompanyDomainObjectManager#addSCCompanyRole(java.lang.Long, java.lang.String, java.lang.String)
	 * 
	 */
	public void addSCCompanyRole(Long roleId, String trakheesId, String strUserName) {

		this.singleClientCompanyRoleDAO.addOrUpdate(roleId, trakheesId, strUserName, ConstantsCommon.CLIENT_ROLE_STATUS_ACTIVE);

	}

	/**
	 * findTrakheesIdAndPinByEmail
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientCompanyDomainObjectManager#findTrakheesIdAndPinByEmail(java.lang.String)
	 * 
	 */
	public TrakheesIdAndPINVO findTrakheesIdAndPinByEmail(String email) {

		return this.singleClientCompanyDAO.findTrkheesIdAndPinByEmail(email);
	}

	/**
	 * validateLicense
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientCompanyDomainObjectManager#validateLicense(ae.pcfc.etks.online.common.vo.LicenseVO)
	 * 
	 */
	public boolean validateLicense(LicenseVO licenseVO) {

		return this.singleClientCompanyDAO.validateLicense(licenseVO);
	}

	/**
	 * findSCClientCompanyByVO
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientCompanyDomainObjectManager#findSCClientCompanyByVO(ae.pcfc.etks.online.common.vo.NonClientCompanyVO)
	 * 
	 */
	public List<TTscClientCompany> findSCClientCompanyByVO(NonClientCompanyVO nonClientCompanyVO) {
		return this.singleClientCompanyDAO.findSCCompanyByVO(nonClientCompanyVO);
	}

	/**
	 * validateClientName
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientCompanyDomainObjectManager#validateClientName(ae.pcfc.etks.online.common.vo.ClientNameVO)
	 * 
	 */
	public boolean validateClientName(ClientNameVO clientNameVO) {
		return this.singleClientCompanyDAO.validateClientName(clientNameVO);
	}

	/**
	 * getClientCategory
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientCompanyDomainObjectManager#getClientCategory(java.lang.String)
	 * 
	 */
	public List<TTscClientCategory> getClientCategory(String clientType) throws LookupException {
		
		return this.singleClientCompanyDAO.getClientCategory(clientType);
	}

	/**
	 * getClientCategoryById
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientCompanyDomainObjectManager#getClientCategoryById(java.lang.String)
	 * 
	 */
	public TTscClientCategory getClientCategoryById(String categoryId) throws LookupException {
		
		return this.singleClientCompanyDAO.getClientCategoryById(categoryId);
	}

	/**
	 * getClientLicenseTypeByCode
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientCompanyDomainObjectManager#getClientLicenseTypeByCode(java.lang.String)
	 * 
	 */
	public TTscLicType getClientLicenseTypeByCode(String licenseTypeCode) throws LookupException {
		
		return this.singleClientCompanyDAO.getClientLicenseTypeByCode(licenseTypeCode);
	}
	
	/**
	 * searchCompany
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientCompanyDomainObjectManager#searchCompany(ae.pcfc.etks.online.common.vo.SearchCriteriaCompanyVO)
	 * 
	 */
	public List<TTscClientCompany> searchCompany(SearchCriteriaCompanyVO searchCriteriaCompanyVO) {
		return this.singleClientCompanyDAO.searchCompany(searchCriteriaCompanyVO);
	}

	/**
	 * findActivitiesOfClientCompany
	 * 
	 * @see ae.pcfc.etks.online.common.domain.manager.SingleClientCompanyDomainObjectManager#findActivitiesOfClientCompany(ae.pcfc.etks.online.common.vo.SearchCriteriaActivityVO)
	 * 
	 */
	public List<ActivityVO> findActivitiesOfClientCompany(SearchCriteriaActivityVO searchCriteriaActivityVO) {
		// TODO Auto-generated method stub
		return this.singleClientCompanyDAO.findActivitiesOfClientCompany(searchCriteriaActivityVO);
	}

	public void removeSCCompanyRole(Long roleId, String cTrakheesId, String strUserName) {
		this.singleClientCompanyRoleDAO.addOrUpdate(roleId, cTrakheesId, strUserName, ConstantsCommon.CLIENT_ROLE_STATUS_INACTIVE);		
	}

	public void undoCreateCompany(ClientCompanyReq companyReq, String strTrakheesId) {
		if(companyReq != null){
			// Disabling the Roles in the TSC
			if(companyReq.getClientCompanyRoleReqs() != null){
				for (ClientCompanyRoleReq companyRole : companyReq.getClientCompanyRoleReqs()) {
					this.singleClientCompanyRoleDAO.addOrUpdate(new Long(companyRole.getCsrReqId()), strTrakheesId, companyReq.getCpCreatedUser(), ConstantsCommon.CLIENT_ROLE_STATUS_INACTIVE);
				}
			}
			String rows = singleClientCompanyDAO.updateClientCompanyStatus(companyReq, strTrakheesId);
		}
	}
}
